home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.02 Feb 95 / Yenta / Erics C++ Libraries / PPC Classes / CPPConnectTask.cp next >
Encoding:
Text File  |  1996-04-04  |  4.4 KB  |  163 lines  |  [TEXT/KAHL]

  1. /********************************************************* DEFINITION
  2.     DATE:    10/20/93
  3.     AUTHOR: Eric R. Rosé
  4.  
  5.     CLASS:  CPPConnectTask
  6.     
  7.     SUPERCLASS: CPPPeriodicTask
  8.     
  9.         This C++ class lets you open a connection with another 
  10.         network entity
  11.     
  12. ********************************************************************/
  13.  
  14. #include <CPPConnectTask.h>
  15. #include <CPPTaskManager.h>
  16. #include <CPPNodeInfo.h>
  17. #include <MemoryTools.h>
  18. #include <script.h>
  19. #include <StringTools.h>
  20.  
  21. /*-----------------------------------------------------------------*/
  22. /*------------------------ PUBLIC METHODS -------------------------*/
  23. /*-----------------------------------------------------------------*/
  24.  
  25.     CPPConnectTask::CPPConnectTask (CPPTaskManager *TaskManager, 
  26.                                     long minPeriod, 
  27.                                     Boolean deleteWhenDone) :
  28.                     CPPPeriodicTask (TaskManager, minPeriod,
  29.                                        deleteWhenDone)
  30.     {
  31.         connectTo = NULL;
  32.         startRec = NULL;
  33.         location = NULL;
  34.         portRec = NULL;
  35.     }
  36.  
  37. /*-----------------------------------------------------------------*/
  38.  
  39.     CPPConnectTask::~CPPConnectTask (void)
  40.     {
  41.         NukePtr(location);
  42.         NukePtr(startRec);
  43.         NukePtr(portRec);
  44.     }
  45.     
  46. /*-----------------------------------------------------------------*/
  47.     
  48.     char     *CPPConnectTask::ClassName (void)
  49.     {
  50.         return "CPPConnectTask";
  51.     }
  52.     
  53. /*-----------------------------------------------------------------*/
  54.  
  55.     void    CPPConnectTask::DoPeriodicAction (void)
  56.     /* store the error code and session ID which was returned */
  57.     {
  58.         if (this->startRec->ioResult != 1)
  59.           {
  60.               this->callResult = startRec->ioResult;
  61.               this->hasCompleted = TRUE;
  62.               if (this->callResult == noErr)
  63.                 this->sessionID = this->startRec->sessRefNum;
  64.           }
  65.     }
  66.     
  67. /*-----------------------------------------------------------------*/
  68.  
  69.     void    CPPConnectTask::DoCompletedAction (void)
  70.     {
  71.         CPPPeriodicTask::DoCompletedAction();
  72.         NukePtr(location);
  73.         NukePtr(portRec);
  74.         NukePtr(startRec);
  75.     }
  76.     
  77. /*-----------------------------------------------------------------*/
  78.     
  79.     PPCSessRefNum    CPPConnectTask::GetSessionID (Boolean *isDone)
  80.     {
  81.         if ((*isDone = this->hasCompleted) == TRUE)
  82.           return this->sessionID;
  83.         else
  84.           return 0;
  85.     }
  86.     
  87. /*-----------------------------------------------------------------*/
  88.     
  89.     void     CPPConnectTask::StartConnectTask (PPCPortRefNum SourcePortRefNum,
  90.                                               CPPNodeInfo *ConnectTo,
  91.                                               CompletionProc DoProc)
  92.     {
  93.         StringPtr        ObjStr = NULL, TypeStr = NULL, ZoneStr = NULL;
  94.         EntityName        theName;
  95.         StringPtr        PName;
  96.         short            i = 1;
  97.  
  98.         if (!this->hasCompleted)
  99.           return;
  100.           
  101.         this->sessionID = 0;
  102.  
  103.         NukePtr(startRec);
  104.         connectTo = ConnectTo;
  105.         
  106.         startRec = (PPCStartPBPtr)NewPtrClear(sizeof(PPCStartPBRec));
  107.         if (startRec)
  108.           {
  109.               location = (LocationNamePtr)NewPtrClear(sizeof(LocationNameRec));
  110.               if (location)
  111.                 {
  112.                     portRec = (PortInfoRec *)NewPtrClear(sizeof(PortInfoRec));
  113.                 if (portRec)
  114.                   {
  115.                       PName = portRec->name.name;
  116.                     // fill in the Location Rec with data from the SendTo object;
  117.                     ConnectTo->GetNodeName (&ObjStr, &TypeStr, &ZoneStr);
  118.                     CopyString (ObjStr, theName.objStr);
  119.                     CopyString (TypeStr, theName.typeStr);
  120.                     CopyString (ZoneStr, theName.zoneStr);
  121.                     location->locationKindSelector = ppcNBPLocation;
  122.                     location->u.nbpEntity = theName;
  123.             
  124.                     // fill in the PPCPortRec with the destination port name
  125.                     portRec->name.nameScript = smRoman;
  126.             
  127.                     CopyString(TypeStr, PName);
  128.                     while (((PName[i] & 0x00FF) != 0xA5) && (i <= PName[0]))
  129.                       i++;
  130.                     PName[0] = i-1;
  131.                     portRec->name.portKindSelector = ppcByString;
  132.                     CopyString ("\pPPCCommPort", portRec->name.u.portTypeStr);
  133.             
  134.                     // fill in the 'start session' record
  135.                     startRec->ioCompletion = NULL;
  136.                     startRec->portRefNum = SourcePortRefNum;
  137.                     startRec->serviceType = ppcServiceRealTime;
  138.                     startRec->resFlag = 0;
  139.                     startRec->portName = &portRec->name;
  140.                     startRec->locationName = location;
  141.                     startRec->userData = 0;
  142.                     startRec->userRefNum = 0;        // guest access
  143.                     
  144.                     // Try to open the connection asynchronously
  145.                     if ((this->callResult =  PPCStart (startRec, TRUE)) == noErr)
  146.                       {    // add the task, if the call does not error-out
  147.                         this->hasCompleted = FALSE;
  148.                         this->ourManager->AddPeriodicTask(this);
  149.                           return;
  150.                       }
  151.                   }
  152.                 }
  153.           }
  154.           
  155.         // we will only get here if all memory allocations fail, or
  156.         // the call itself fails
  157.         NukePtr(startRec);
  158.         NukePtr(location);
  159.         NukePtr(portRec);
  160.     }
  161.     
  162. /*-----------------------------------------------------------------*/
  163.